﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class MotobikeManagerController : Controller
    {
        //
        // GET: /MotobikeManager/

        Fcom.VNInsurance.Web.Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
         //
        public ActionResult GetJsonSubs()
        {
            var ins = from m in db.MotobikeSubmoneys orderby m.MotoSubmoney select new { m.ID, m.MotoSubmoney };
            return Json(ins, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SubMoney()
        {
            return View();
        }
        public ActionResult AddSubs()
        {
            return View();
        }
        public ActionResult EditSubs(int ID)
        {
            var md = db.MotobikeSubmoneys.Where(m => m.ID == ID).FirstOrDefault();
            return View(md);
        }
        public ActionResult DeleteSubByID(int ID)
        {
            var md = db.MotobikeSubmoneys.Where(m => m.ID == ID).FirstOrDefault();
            db.MotobikeSubmoneys.DeleteOnSubmit(md);
            db.SubmitChanges();
            return Json(new { success = true },JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult EditSubs(Models.MotobikeSubmoney model)
        {
            if (ModelState.IsValid)
            {
                var md = db.MotobikeSubmoneys.Where(m => m.ID == model.ID).FirstOrDefault();
                md.MotoSubmoney = model.MotoSubmoney;

                db.SubmitChanges();
                return RedirectToAction("SubMoney");
            }
            return View();
        }
        [HttpPost]
        public ActionResult AddSubs(Models.MotobikeSubmoney model)
        {
            if (ModelState.IsValid)
            {
                Models.MotobikeSubmoney md = new MotobikeSubmoney();
                md.MotoSubmoney = model.MotoSubmoney;
                db.MotobikeSubmoneys.InsertOnSubmit(md);
                db.SubmitChanges();
                return RedirectToAction("SubMoney");
            }
            return View();
        }
         //
        public ActionResult GetJsonIns()
        {
            var ins = from m in db.MotobikeInsMoneys orderby m.InsuanceMoney select new { m.ID,m.InsuanceMoney};
            return Json(ins,JsonRequestBehavior.AllowGet);
        }
        public ActionResult InsuranceMoney()
        {
            return View();
        }
        public ActionResult AddIns()
        {
            return View();
        }
        public ActionResult EditIns(int ID)
        {
            var md = db.MotobikeInsMoneys.Where(m => m.ID == ID).FirstOrDefault();
            return View(md);
        }
        public ActionResult DeleteInsByID(int ID)
        {
            var md = db.MotobikeInsMoneys.Where(m => m.ID == ID).FirstOrDefault();
            db.MotobikeInsMoneys.DeleteOnSubmit(md);
            db.SubmitChanges();
            return Json(new { success=true},JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult EditIns(Models.MotobikeInsMoney model)
        {
            if (ModelState.IsValid)
            {
                var md = db.MotobikeInsMoneys.Where(m => m.ID == model.ID).FirstOrDefault();
                md.InsuanceMoney = model.InsuanceMoney;
            
                db.SubmitChanges();
                return RedirectToAction("InsuranceMoney");
            }
            return View();
        }
         [HttpPost]
        public ActionResult AddIns(Models.MotobikeInsMoney model)
        {
            if (ModelState.IsValid)
            {
                Models.MotobikeInsMoney md = new MotobikeInsMoney();
                md.InsuanceMoney = model.InsuanceMoney;
                db.MotobikeInsMoneys.InsertOnSubmit(md);
                db.SubmitChanges();
                return RedirectToAction("InsuranceMoney");
            }
            return View();
        }
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult AddMotobike()
        {
            ViewBag.AutoCategory = (from a in db.AutoCategories select a);
            return View();
        }
        [HttpPost]
        public ActionResult AddMotobike(Motobike moto)
        {
            Motobike c = new Motobike();
            var isexit = db.Motobikes.Where(m => m.Name == moto.Name).FirstOrDefault();
            if (isexit == null)
            {
                c.AutoCatID = moto.AutoCatID;
                c.Name = moto.Name;
                c.CC = moto.CC;
                db.Motobikes.InsertOnSubmit(c);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        public JsonResult listmoto()
        {
            var autocatlist = (from a in db.Motobikes
                               select new
                               {
                                   MotoID = a.ID,
                                   MotoName = a.Name,
                                   AutoName = a.AutoCategory.Name
                                   ,a.CC
                               });
            return Json(autocatlist, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult EditMoto(int id)
        {
            ViewBag.AutoCategory = (from a in db.AutoCategories select a);
            var moto = db.Motobikes.Where(m => m.ID == id).FirstOrDefault();
            try
            {
                return View(moto);

            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public ActionResult EditMoto(Motobike moto)
        {
            var foud = db.Motobikes.Where(m => m.ID == moto.ID).FirstOrDefault();
            if (foud != null)
            {
                foud.Name = moto.Name;
                foud.AutoCatID = moto.AutoCatID;
                foud.CC = moto.CC;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        [HttpPost]
        public ActionResult removeMoto(int id)
        {
            var at = db.Motobikes.Where(m => m.ID == id).FirstOrDefault();
            db.Motobikes.DeleteOnSubmit(at);
            db.SubmitChanges();
            return Json(true);
        }


    }
}
